package com.xiaoyu.izf.controller;

import cn.hutool.crypto.SecureUtil;
import com.xiaoyu.izf.aspect.RequestAuth;
import com.xiaoyu.izf.model.UserInfo;
import com.xiaoyu.izf.model.page.*;
import com.xiaoyu.izf.config.SysConfig;
import com.xiaoyu.izf.service.UserInfoService;
import com.xiaoyu.izf.util.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/admin_user")
public class AdminUserController extends BaseController{
	
	@Autowired
	private UserInfoService userInfoService;

	@RequestMapping("/login")
	@ResponseBody
	public String login(HttpServletRequest request, String userName, String pwd, String myCode) {
		if(StringUtils.isEmpty(userName)|| StringUtils.isEmpty(pwd)){
			return "<script>parent.callBack('用户名或密码不能为空。');</script>";
		}
		
		if(SysConfig.isNeedCheckCode==1){
			if(StringUtils.isEmpty(myCode)){
				return "<script>parent.callBack('请输入验证码。');</script>";
			}
			String codeSession = (String)request.getSession().getAttribute("BarPriRandCheckCode");
			if(codeSession!=null){
				codeSession=codeSession.toLowerCase();
			}else{
				return "<script>parent.callBack('验证码已过期。');</script>";
			}
			if(!myCode.toLowerCase().equals(codeSession)){
				return "<script>parent.callBack('验证码错误。');</script>";
			}
		}
		UserInfo admins = userInfoService.adminUserByUserName(userName);
		if(admins!=null){
			pwd = SecureUtil.md5(pwd+ SysConfig.pwdEx);
			if(pwd.equals(admins.getPwd())){
				request.getSession().setAttribute(SysConfig.user_info_in_session_key, admins);
			}else {
				request.getSession(true).setAttribute("BarPriRandCheckCode", null); 
				return "<script>parent.callBack('密码不正确。');</script>";
			}
		}else{
			request.getSession(true).setAttribute("BarPriRandCheckCode", null); 
			return "<script>parent.callBack('用户不存在~！');</script>";
		}
		return "<script>parent.callBack('ok');</script>";
	}
	
	@RequestMapping("/log_out")
	public String logOut(HttpServletRequest request) {
		request.getSession().setAttribute(SysConfig.user_info_in_session_key, null);
		return "redirect:/login_page";
	}
	
	@RequestMapping("/pwd_mod_page")
	public String pwdModPage(HttpServletRequest request) {
		return "/mgr/admin/pwd_mod";
	}
	
	@RequestMapping("/pwd_mod")
	@ResponseBody
	@RequestAuth
	public FormResultBean pwdMod(HttpServletRequest request) {
		FormResultBean fr = new FormResultBean();
		fr.setStatus(Status.HTTP_OK.value());
		UserInfo user = (UserInfo)request.getAttribute("user");
		String pwd = ParamUtil.getString(request, "pwd");
		String newPwd = ParamUtil.getString(request, "new_pwd");
		String newPwd2 = ParamUtil.getString(request, "new_pwd2");
		if(newPwd.equals(newPwd2)&&!StringUtils.isEmpty(newPwd)){
			UserInfo admins = userInfoService.adminUserByUserName(user.getUserName());
			pwd = SecureUtil.md5(pwd+ SysConfig.pwdEx);
			if(pwd.equals(admins.getPwd())){
				admins.setPwd(SecureUtil.md5(newPwd+ SysConfig.pwdEx));
				userInfoService.adminUserSave(admins);
				fr.setType("success");
				fr.setMessage("修改成功，请重新登录。");
				return fr;
			}else {
				fr.setType("error");
				fr.setMessage("原始密码不正确。");
				return fr;
			}
		}else{
			fr.setType("error");
			fr.setMessage("2次输入密码不一致。");
			return fr;
		}
		
	}
	
	
	@RequestMapping("/list_page")
	public String listPage(HttpServletRequest request) {
		return "/mgr/admin/admin_list";
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public DataTableBean list(HttpServletRequest request, @RequestBody String body) throws IOException {
		UserInfo user = user(request);
		ParamListBean paramListBean = getParamListBean(body);
		DataTableBean dataTableBean = userInfoService.list(paramListBean,user);
		return dataTableBean;
	}
	
	@RequestMapping("/add_page")
	public String adminAddPage(HttpServletRequest request) {
		return "/mgr/admin/admin_add";
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public FormResultBean adminAdd(HttpServletRequest request, UserInfo userInfo) {
		FormResultBean fr = new FormResultBean();
		fr.setStatus(Status.HTTP_OK.value());
		UserInfo adminUser2 = userInfoService.adminUserByUserName(userInfo.getUserName());
		if(adminUser2!=null){
			fr.setType("error");
			fr.setMessage("该用户名称已被占用。");
			return fr;
		}

		fr.setType("success");
		fr.setMessage("添加成功。");
		return fr;
	}
	
	@RequestMapping("/mod_page")
	public String adminModPage(HttpServletRequest request, int id, Model model) {
		UserInfo adminUser = userInfoService.adminUserOne(id);
		model.addAttribute("user",adminUser);
		return "/mgr/admin/admin_mod";
	}
	
	@RequestMapping("/mod")
	@ResponseBody
	public FormResultBean adminMod(HttpServletRequest request, UserInfo adminUser) {
		FormResultBean fr = new FormResultBean();
		fr.setStatus(Status.HTTP_OK.value());

		fr.setType("success");
		fr.setMessage("修改成功。");
		return fr;
	}
	
	@RequestMapping("/my_nav")
	@RequestAuth
	public String nav(HttpServletRequest request,Model model) {
		UserInfo admin = user(request);
		model.addAttribute("user",admin);
		if(admin.getIsAdmin()==0){//合伙人
			return "/mgr/inc/nav1";
		}
		if (admin.getIsAdmin()==1){
			return "/mgr/inc/nav";//管理员
		}
		return "";
	}
	
}
